package com.hlhj.funlike_business.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.adapter.PreferentialAdp;
import com.hlhj.funlike_business.bean.ActivityBean;
import com.hlhj.funlike_business.presenter.PreferentialPresenter;
import com.hlhj.funlike_business.views.PreferentialView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class PreferentialAty extends BaseActivity<PreferentialView,PreferentialPresenter> implements PreferentialView {

    private RecyclerView recycle_list;
    private EditText ed_man,ed_jian;
    private ImageView img_add,img_back;
    private TextView tv_title;

    private PreferentialAdp preferentialAdp;
    private List<ActivityBean.DataBean> strings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setType(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    @Override
    protected int getContentId() {
        return R.layout.aty_preferential;
    }

    @Override
    protected void initData() {
        tv_title.setText("店铺活动");
        strings = new ArrayList<>();
        preferentialAdp = new PreferentialAdp(strings);
        recycle_list.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        recycle_list.setAdapter(preferentialAdp);
    }

    @NotNull
    @Override
    public PreferentialView bindView() {
        return this;
    }

    @NotNull
    @Override
    public PreferentialPresenter createPresenter() {
        return new PreferentialPresenter();
    }

    @Override
    public void initView() {
        img_back = findViewById(R.id.img_back);
        tv_title = findViewById(R.id.tv_title);
        recycle_list = findViewById(R.id.recycle_list);
        ed_man = findViewById(R.id.ed_man);
        ed_jian = findViewById(R.id.ed_jian);
        img_add = findViewById(R.id.img_add);
    }

    @Override
    public void logicStart() {
        getPresenter().LoadActivityList(PreferentialAty.this);

        preferentialAdp.setListener(new PreferentialAdp.delActivity() {
            @Override
            public void showResult(int id) {
                getPresenter().LoadDelActivity(PreferentialAty.this,id);
            }
        });
    }

    @Override
    public void setListener() {
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        img_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null ==ed_man.getText().toString()|| "".equals(ed_man.getText().toString()) || null == ed_jian.getText().toString() || "".equals(ed_jian.getText().toString())){
                    Toast.makeText(PreferentialAty.this,"请输入满减金额",Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    getPresenter().LoadAddActivity(PreferentialAty.this,ed_man.getText().toString(),ed_jian.getText().toString());
                }
            }
        });
    }

    @Override
    public void showResult(String msg) {
        Toast.makeText(PreferentialAty.this,msg.toString(),Toast.LENGTH_SHORT).show();
        getPresenter().LoadActivityList(PreferentialAty.this);

        ed_man.setText("");
        ed_jian.setText("");
    }

    @Override
    public void showList(List<ActivityBean.DataBean> dataBeans) {
        strings.clear();
        strings.addAll(dataBeans);
        preferentialAdp.notifyDataSetChanged();
    }
}
